package com.cola.dao;

import com.cola.entity.order.OrderMessage;
import com.cola.utils.DBControl;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.util.List;

/**
 * 消息Dao
 * @author Max
 * @since 2017/9/18
 */
public class MessageDao extends BaseDao{
    private final Logger logger =  LoggerFactory.getLogger(getClass());

    private static MessageDao MessageDao = new MessageDao();

    private MessageDao(){}

    public static MessageDao getInstance(){
        return MessageDao;
    }


    /**
     * 获取消息列表
     * @param mepNo 经销商编号
     * @param beginNo 起始位置
     * @param pageSize 页面大小
     * @return List<OrderMessage>
     * @author Max
     * @since 2017/9/18
     */
    public List<OrderMessage> getOrderMessages(String mepNo,Integer beginNo,Integer pageSize){
        Connection conn = null;
        try{
            conn = DBControl.getConnection();

            String sql = "select * from cola_order_message where mep_no = ? and status =1 order by check_status asc,create_time desc limit ?,?";

            return getQueryRunner().query(conn,sql,new BeanListHandler<OrderMessage>(OrderMessage.class),mepNo,beginNo,pageSize);

        }catch (Exception e){
            logger.error("获取消息列表异常！",e);
        }finally {
            DbUtils.closeQuietly(conn);
        }
        return null;
    }





    /**
     * 添加订单消息
     * @param message
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/18
     */
    public boolean addOrderMessage(OrderMessage message){
        Connection conn = null;
        try{
            conn = DBControl.getConnection();
            String sql = "insert into cola_order_message(order_no,mep_no,message_content,create_time)" +
                    " values(?,?,?,?)";
            return getQueryRunner().update(conn,sql,message.getOrder_no(),message.getMep_no(),message.getMessage_content(),message.getCreate_time())>0;
        }catch (Exception e){
            logger.error("添加订单消息异常！",e);
        }finally {
            DbUtils.closeQuietly(conn);
        }
        return false;
    }


    /**
     * 更新消息阅读状态
     * @param message
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/18
     */
    public boolean updateOrderMessageIsRead(OrderMessage message){
        Connection conn = null;
        try{
            conn = DBControl.getConnection();
            String sql = "update cola_order_message set is_read =? where message_id =?";
            return getQueryRunner().update(conn,sql,message.getIs_read(),message.getMessage_id())>0;
        }catch (Exception e){
            logger.error("更新消息阅读状态异常！",e);
        }finally {
            DbUtils.closeQuietly(conn);
        }
        return false;
    }


    /**
     * 更新消息处理状态
     * @param message
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/18
     */
    public boolean updateOrderMessageCheckStatus(OrderMessage message){
        Connection conn = null;
        try{
            conn = DBControl.getConnection();
            String sql = "update cola_order_message set check_status =? where message_id =?";
            return getQueryRunner().update(conn,sql,message.getCheck_status(),message.getMessage_id())>0;
        }catch (Exception e){
            logger.error("更新消息处理状态异常！",e);
        }finally {
            DbUtils.closeQuietly(conn);
        }
        return false;
    }

}
